package com.example.Dietproject.vegetables.controller;

import com.alibaba.fastjson.JSON;
import com.example.Dietproject.vegetables.entity.Diet_vegetables;
import com.example.Dietproject.vegetables.servlce.Diet_VegetableServlce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/vegetables")
public class Diet_vegetablesControoler {

    @Autowired
    private Diet_VegetableServlce diet_vegetableServlce;
    @RequestMapping("/VegetablesGetAll")
    public String getAll(Model model){
        try {
            List<Diet_vegetables> diet_vegetables =  diet_vegetableServlce.selectByAll();
            model.addAttribute("diet_vegetables",diet_vegetables);
            return  "type";
        }catch (Exception e){
            e.getMessage();
        }
        return null;
    }
    //跳转菜品
    @RequestMapping("/inventory")
    public String inventory(){

        return "inventory";
    }
    //根据id查询出来
    @RequestMapping("/vegetabupdate")
    public String vegetabupdate(Integer id,Model model){
        try {
            Diet_vegetables diet_vegetables = diet_vegetableServlce.selectByPrimaryKey(id);
            model.addAttribute("diet_vegetables",diet_vegetables);
        }catch (Exception e){
            e.getMessage();
        }
        return "vegetabupdate";
    }

    //修改分类
    @RequestMapping("/vegetamodify")
    public String vegetamodify(Diet_vegetables diet_vegetables,Model model){
        try {
            int count = diet_vegetableServlce.updateByPrimaryKeySelective(diet_vegetables);
        }catch (Exception e){
            e.getMessage();
        }
        return "type"; //修改成功跳查询
    }
    //删除分类
    @RequestMapping("/DeleteRawMaterialsType")
    public String DeleteRawMaterialsType(int ificationid,Model model){
        try {
            int count = diet_vegetableServlce.deleteByPrimaryKey(ificationid);
        }catch (Exception e){
            e.getMessage();
        }
        return "type";   //删除跳查询页面
    }
    //新增分类
    @ResponseBody
    @RequestMapping("/AddRawMaterialsType")
    public String addUser1Submit(Diet_vegetables diet_vegetables,Model model){
        Integer cunt =  diet_vegetableServlce.insertSelective(diet_vegetables);

        String JSON = com.alibaba.fastjson.JSON.toJSONString(cunt);

        return JSON;
    }
}
